package com.xyh.springboot.exception;

import com.xyh.springboot.util.ResultCode;
import com.xyh.springboot.util.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author:kyle
 * @Date:2020/12/28 - 17:41
 * @Project:springboot
 * @Package:com.xyh.springboot.exception
 * @Version:1.0
 */
//@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionConfig {

    private Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {BizException.class})
    public ResultVO bizExceptionHandler(BizException e){
        log.error("发生业务异常，原因是：" + e.getMessage());
        return ResultVO.fail(e.getStatus(),e.getMessage());
    }

    /**
     * 处理空指针的异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {NullPointerException.class})
    public ResultVO exceptionHandler(NullPointerException e){
        log.error("发生空指针异常！原因是:"+ e);
        return ResultVO.fail(ResultCode.BODY_NOT_MATCH);
    }

    /**
     * 其他异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    public ResultVO exceptionHandler(Exception e){
        log.error("未知异常！原因是:"+ e);
        return ResultVO.fail(ResultCode.INTERNAL_SERVER_ERROR);
    }
}
